<template>
  <!-- 礼品卡编辑 -->
  <div class="gift_card_edit">
    <div class="bor add_con_item bg_write mar_bottom_25">
      <h5 style="text-align: left;">礼品卡编辑</h5>
      <el-form :label-position="labelPosition" label-width="90px" :model="formGift" ref="formLabelAlign">
        <div v-if="this.$route.query.id">
          <el-form-item label="排序">
            <el-col :span="12">
              <el-input type="text" :rows="6" clearable placeholder="请输入" v-model="formGift.sort" autocomplete="off"></el-input>
            </el-col>
          </el-form-item>
          <el-form-item label="是否显示">
            <el-col :span="2">
              <el-switch v-model="formGift.show" active-color="#13ce66" inactive-color="#999999" hone_switch :width="70"
                active-value="1" inactive-value="0"></el-switch>
            </el-col>
          </el-form-item>
        </div>
        <el-form-item label="标题">
          <el-col :span="12">
            <el-input type="text" :rows="6" clearable placeholder="请输入" v-model="formGift.title" autocomplete="off"></el-input>
          </el-col>
        </el-form-item>
        <el-form-item label="图片">
          <el-col :span="12">
            <el-input v-model="formGift.piclink" autocomplete="off" disabled="disabled" class="mar_bottom_5"></el-input>
            <uploadpic :piclink="formGift.piclink" :width="300" :height="175" :is_more="false" @send_pic="get_pic"></uploadpic>
          </el-col>
        </el-form-item>
        <el-form-item label="价格">
          <el-col :span="12">
            <el-input type="text" :rows="6" clearable placeholder="请输入" v-model="formGift.price" autocomplete="off"></el-input>
          </el-col>
        </el-form-item>
        <el-form-item label="成本价">
          <el-col :span="12">
            <el-input type="text" :rows="6" clearable placeholder="请输入" v-model="formGift.cost" autocomplete="off"></el-input>
          </el-col>
        </el-form-item>
        <el-form-item label="面值">
          <el-col :span="12">
            <el-input type="text" :rows="6" clearable placeholder="请输入" v-model="formGift.money" autocomplete="off"></el-input>
          </el-col>
        </el-form-item>
        <el-form-item label="说明">
          <el-col :span="16">
            <div>
              <ue v-model="formGift.content" v-loading="loadding1" v-if="!loadding1" :value="formGift.content"
                upload-url="/api/common/upload/index/" fileName="file" />
              <div class="blank"></div>
            </div>
          </el-col>
        </el-form-item>
        <el-form-item>
          <el-col :span="24" class="tc">
            <el-button :loading="$store.state.isLoading" type="primary" @click="subForm">保存</el-button>
          </el-col>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
  import uploadpic from "@/components/page/pic/up_pic";
  import ue from "@/components/currency/Ue/ue.vue";
  export default{
    data(){
      return{
        labelPosition: "right",
        formGift: {
          title: "",
          piclink: "",
          price: "0",
          cost: "0",
          money: "0",
          content: ""
        },
        loadding1: true
      }
    },
    components: {
      uploadpic,
      ue
    },
    methods: {
      get_pic(pic) {
        this.formGift.piclink = pic;
      },
      getGift(){
        //查看礼品卡信息
        var id = this.$route.query.id;
        if (id) {
          this.$api.getShops.getGiftCardById({
            id: id
          }).then(res => {
            if (res.data.code == 200) {
              this.formGift = res.data.result;
              this.loadding1 = false;
            }
          })
        }
      },
      subForm() {
        //保存
        if (this.$route.query.id) {
          //编辑
          this.$api.getShops.editGiftCard(this.formGift).then(res => {
            if (res.data.code == 200) {
              this.$message.success("修改成功");
              setTimeout(() => {
                this.$router.push("/index/shop/gift_card");
              })
            }else{
              this.$fnc.alertError(res.data.result);
            }
          })
        } else {
          this.$api.getShops.addGiftCard(this.formGift).then(res => {
            if (res.data.code == 200) {
              this.$message.success("添加成功");
              setTimeout(() => {
                this.$router.push("/index/shop/gift_card");
              })
            }else{
              this.$fnc.alertError(res.data.result);
            }
          })
        }
      }
    },
    created() {
      this.loading = false;
      if(this.$route.query.id){
        this.getGift();
      }else{
        this.loadding1 = false;
      }
    }
  }
</script>

<style>
</style>
